package wcg;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;


public class DlgSelectRecentFile extends JDialog {

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			DlgSelectRecentFile dialog = new DlgSelectRecentFile();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	private JList stringList;
	private DefaultListModel  model = new DefaultListModel();
	public boolean Ok=false;
	/**
	 * Create the dialog.
	 */
	public DlgSelectRecentFile() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(DlgSelectRecentFile.class.getResource("/images/winchkg3-icon.png")));
		setSize(new Dimension(400, 300));
		setTitle("Recent files...");
		//setBounds(100, 100, 587, 201);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setMinimumSize(new Dimension(200, 20));
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		JButton okButton = new JButton("OPEN");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Ok=true;
				dispose();
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		stringList = new JList(model);
		//panel.add(stringList, BorderLayout.CENTER);
		stringList.setBounds(0, 0, 200, 200);
		//panel.add(stringList);
		stringList.setForeground(Color.DARK_GRAY);
		stringList.setVisibleRowCount(0);
		stringList.setToolTipText("Select file(s) ");
		stringList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Ok=true;
					dispose();
				}
			}
		});
		
		
		JScrollPane sp = new JScrollPane();
		sp.getViewport().setView(stringList);
		sp.setPreferredSize(new Dimension(200, 80));
		panel.add(sp, BorderLayout.CENTER);
		
	}
	public void addData(String mes){
		if(mes==null) return ;
		model.addElement(new String(mes));
		int xpix=mes.length()*8;
		int ypix=model.getSize()*12+150;
		Dimension dm = getSize();
		if(dm.width > xpix){
			xpix = dm.width;
		}
		if(dm.height > ypix){
			ypix = dm.height;
		}
		setSize(new Dimension(xpix,ypix));
		
	}
	public int[] getIndeces(){
		return stringList.getSelectedIndices();
	}
}
